package com.es.code.utils;

import com.es.code.annotation.LogAround;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.IndexQuery;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author jichf
 * @Description
 * @create 2021-11-06 13:47
 */

@Component
@EnableAsync
public class ElasticSearchUtils {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * logger
     */
    private Logger logger = LoggerFactory.getLogger(ElasticSearchUtils.class);

    /**
     * 根据Class 或者 indexName 获取 IndexOperations
     * @param mode ture : Class方式创建，false indexName方式
     * @param cls  Class 对象
     * @param indexNames 索引名称
     * @return
     */
    private IndexOperations getIndexOperations(boolean mode, Class cls, String ...indexNames){
        IndexOperations indexOperations;
        if (mode){
            indexOperations = elasticsearchRestTemplate.indexOps(cls);
        }else {
            IndexCoordinates indexCoordinates = IndexCoordinates.of(indexNames);
            indexOperations = elasticsearchRestTemplate.indexOps(indexCoordinates);
        }
        return indexOperations;
    }

    /**
     * 根据Class 或者 indexName 判断索引是否存在
     * @param mode ture : Class方式创建，false indexName方式
     * @param cls  Class 对象
     * @param indexNames 索引名称
     * @return
     */
    @LogAround("根据实体 class 判断是否存在")
    public boolean existIndex(boolean mode, Class cls, String ...indexNames){
        IndexOperations indexOperations = getIndexOperations(mode, cls, indexNames);
        return indexOperations.exists();
    }

    /**
     * 根据Class 或者 indexName 创建索引
     * @param mode ture : Class方式创建，false indexName方式
     * @param cls  Class 对象
     * @param indexNames 索引名称
     * @return
     */
    @LogAround("根据索引名称创建索引")
    public boolean createIndex(boolean mode, Class cls, String ...indexNames){
        IndexOperations indexOperations = getIndexOperations(mode, cls, indexNames);
        boolean exists = indexOperations.exists();
        if (exists){
            logger.info("索引已经存在");
            return false;
        }
        boolean create = indexOperations.create();
        boolean mapping = indexOperations.putMapping();

        return create && mapping;
    }


    /**
     * 根据Class 或者 indexName 删除索引
     * @param mode ture : Class方式创建，false indexName方式
     * @param cls  Class 对象
     * @param indexNames 索引名称
     * @return
     */
    @LogAround("根据索引名称删除索引")
    public boolean deleteIndex(boolean mode, Class cls, String ...indexNames){
        IndexOperations indexOperations = getIndexOperations(mode, cls, indexNames);
        if (!indexOperations.exists()){
            logger.info("索引不存在");
        }
        return indexOperations.delete();
    }

    /**
     * 添加一条文档记录
     * @param enity
     * @param <T>
     * @return
     */
    public <T> T addDocument(T enity){
        T save = elasticsearchRestTemplate.save(enity);
        return save;
    }

    /**
     * 添加一条文档记录
     * @param indexName
     * @param id
     * @param source
     */
    public void addDocument(String ...indexNames){
//        IndexQuery query = new IndexQuery();
//        IndexCoordinates indexCoordinates = IndexCoordinates.of(indexNames);
//        elasticsearchRestTemplate.index(query, indexCoordinates);
    }

}
